package com.daxilong.starchen.player.fragment;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;

import android.provider.MediaStore;
import android.view.View;

import com.daxilong.starchen.player.activity.VideoPlayerActivity;
import com.daxilong.starchen.player.adapter.LocalVideoAdapter;
import com.daxilong.starchen.player.databinding.FragmentLocalVideoBinding;
import com.daxilong.starchen.player.model.MediaItem;

import java.util.ArrayList;

/**
 * 本地视频
 */
public class LocalVideoFragment extends BaseFragment {

    private LocalVideoAdapter adapter;
    private ArrayList<MediaItem> mediaItemsList;
    private FragmentLocalVideoBinding binding;

    @Override
    public View getLayout() {
        binding = FragmentLocalVideoBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }

    @Override
    public void initView() {
        adapter = new LocalVideoAdapter();
        binding.recyclerView.setAdapter(adapter);

        adapter.setItemClientListener(new LocalVideoAdapter.MyListOnClickItemListener() {
            @Override
            public void onItemClick(int position) {
//                String str = "https://rzze.oss-cn-shanghai.aliyuncs.com/t/wv.webm";

                Intent intent = new Intent(getContext(), VideoPlayerActivity.class);
                Bundle bundle = new Bundle();
                //intent.setDataAndType(Uri.parse(str),"video/*");
                bundle.putSerializable("videolist",mediaItemsList);
                intent.putExtras(bundle);
                intent.putExtra("position",position);
                getContext().startActivity(intent);

            }
        });
    }

    public void initData(){
        mediaItemsList = new ArrayList<>();

        ContentResolver contentResolver = getContext().getContentResolver();
        Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        String[] objects = {MediaStore.Video.Media.DISPLAY_NAME,
                MediaStore.Video.Media.DURATION,
                MediaStore.Video.Media.SIZE,
                MediaStore.Video.Media.DATA};
        Cursor cursor = contentResolver.query(uri,objects,null,null,null);
        if(cursor != null){
            while ( cursor.moveToNext()){
                MediaItem mediaItem = new MediaItem();
                String name = cursor.getString(0);
                mediaItem.setName(name);
                long duration = cursor.getLong(1);
                mediaItem.setDuration(duration);
                long size = cursor.getLong(2);
                mediaItem.setSize(size);
                String data = cursor.getString(3);
                mediaItem.setData(data);

                mediaItemsList.add(mediaItem);
            }
            cursor.close();;
        }
        adapter.setDataList(mediaItemsList);
    }

}